package com.ruoyi.web.controller.hotel;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.util.ShiroUtils;
import com.xc.hotel.entity.Hotel;
import com.xc.hotel.service.HotelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author hy
 * @version 1.0
 * @date 2020-11-17 15:04
 */
@Controller
@RequestMapping("hotel")
public class HotelController extends BaseController {

    private final String prefix = "hotel/hotel";

    private HotelService hotelService;

    @GetMapping
    @RequiresPermissions("hotel:view")
    public String view(){
        return prefix + "/table";
    }

    @GetMapping("add")
    @RequiresPermissions("hotel:add")
    public String addPage(){
        return prefix + "/add";
    }

    @PostMapping("list")
    @RequiresPermissions("hotel:list")
    @ResponseBody
    public TableDataInfo list(Hotel hotel){
        startPage();
        final List<Hotel> hotelList = hotelService.list(new QueryWrapper<>(hotel));
        return getDataTable(hotelList);
    }

    @PostMapping("add")
    @ResponseBody
    public AjaxResult create(Hotel hotel){
        this.hotelService.save(hotel);
        return AjaxResult.success(hotel);
    }

    @GetMapping("edit/{id}")
    @RequiresPermissions("hotel:edit")
    public String editPage(@PathVariable("id") Integer id, ModelMap modelMap){
        final Hotel hotel = this.hotelService.getById(id);
        modelMap.put("hotel", hotel);
        return prefix + "/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(Hotel hotel){
        hotel.setUpdateTime(new Date());
        hotel.setUpdateBy(ShiroUtils.getLoginName());
        this.hotelService.updateById(hotel);
        return AjaxResult.success();
    }

    @PostMapping("remove")
    @ResponseBody
    public AjaxResult remove(@RequestParam List<Integer> ids){
        if(ids != null && !ids.isEmpty()){
            this.hotelService.removeByIds(ids);
        }
        return AjaxResult.success();
    }

    @Autowired
    public void setHotelService(HotelService hotelService) {
        this.hotelService = hotelService;
    }
}
